﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET;
using GMap.NET.MapProviders;


namespace SecurityDev
{
    public partial class Gmap : Form
    {
        GMapOverlay overlayOne;
        String contry;
        PointLatLng xl = new PointLatLng();        

        public delegate void MapUpdateHandler(object sender, MapUpdateEventArgs e);        

        public event MapUpdateHandler MapUpdated;

        public Gmap()
        {
            InitializeComponent();
        }

        private void mapexplr_Load(object sender, EventArgs e)
        {
            //initialisation de notre map
            mapexplr.SetCurrentPositionByKeywords("Ukraine");
            mapexplr.MapProvider = GMapProviders.BingMap;
            mapexplr.MinZoom = 3;
            mapexplr.MaxZoom = 18;
            mapexplr.Zoom = 6;
            mapexplr.Manager.Mode = AccessMode.ServerAndCache;
        }

        private void Gmap_FormClosing(object sender, FormClosingEventArgs e)
        {            
            if (overlayOne != null && overlayOne.Markers != null && overlayOne.Markers.Count > 0)
            {
                overlayOne.Markers.Clear();
            }
            // instance the event args and pass it each value
            MapUpdateEventArgs args = new MapUpdateEventArgs(xl.Lat.ToString(), xl.Lng.ToString());
            // raise the event with the updated arguments
            MapUpdated(this, args);
            this.Dispose();
        }

        private void mapexplr_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                overlayOne = new GMapOverlay(mapexplr, "OverlayOne");
                //ajout de Markers
                mapexplr.Overlays.Clear();
                mapexplr.Overlays.Add(overlayOne);
                overlayOne.Markers.Add(new GMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(new PointLatLng(mapexplr.FromLocalToLatLng(e.X, e.Y).Lat, mapexplr.FromLocalToLatLng(e.X, e.Y).Lng)));                
                xl = mapexplr.Position;
            }
        }
    }

    // Event on Update Map
    public class MapUpdateEventArgs : EventArgs
    {
        // add local member variables to hold text
        private string Lat;
        private string Lng;

        // class constructor
        public MapUpdateEventArgs(string sLat, string sLng)
        {
            this.Lat = sLat;
            this.Lng = sLng;
        }

        // Properties - Viewable by each listener
        public string getLat
        {
            get
            {
                return Lat;
            }
        }

        public string getLng
        {
            get
            {
                return Lng;
            }
        }

    }
}
